class_name SpawnArea
extends Resource

@export var spawn_area_name:String
@export var spawn_area_type:GameEnum.SpawnAreaType
@export var center:Vector2
@export_group("Rectangle类型")
@export var size:Vector2
@export_group("")
@export_group("Circle类型")
@export var radius:float
@export_group("")
@export var pos_is_integer:bool = false
@export var items:Array[PackedScene]
@export var spawn_infos:Array[SpawnInfo]

func get_spawn_point(cur_owner:Node,parent):
	var attempt_amount = StaticValue.ATTEMPT_AMOUNT
	if spawn_area_type == GameEnum.SpawnAreaType.Rectangle:
		while attempt_amount > 0:
			attempt_amount -= 1
			var pos:Vector2 = Util.get_random_point_in_rectangle(center,size)
			if pos_is_integer:
				pos = floor(pos)
			# 判断是否有碰撞不能生成，生成强制检测对应的obstacle_layer
			var layer_mask = cur_owner.get_entity_data().get_layer_mask_by_tag("Obstacle")
			if Util.check_have_obstacle(parent,GameEnum.UtilObstacleCheckRange.AB,layer_mask,pos + center):
				continue
			return pos + center
	elif spawn_area_type == GameEnum.SpawnAreaType.Circle:
		while attempt_amount > 0:
			attempt_amount -= 1
			var pos = Util.get_random_point_in_circle(center,radius)
			if pos_is_integer:
				pos = floor(pos)
			# 判断是否有碰撞不能生成，生成强制检测对应的obstacle_layer
			var layer_mask = cur_owner.get_entity_data().get_layer_mask_by_tag("Obstacle")
			if Util.check_have_obstacle(parent,GameEnum.UtilObstacleCheckRange.AB,layer_mask,pos + center):
				continue
			return pos + center
func get_item():
	return Util.random_get_count(items,1,true)
# 该 Tween 将在下一个处理帧或物理帧时自动开始（取决于 Tween.TweenProcessMode）
func get_spawn_task(cur_owner:Node,spawn_item:Callable) -> Tween:
	var spawn_task_tween:Tween = cur_owner.create_tween()
	for info:SpawnInfo in spawn_infos:
		var amount = GameManager.rng_randi_range(info.spawn_amount_range.x,info.spawn_amount_range.y)
		for i in range(amount):
			spawn_task_tween.tween_callback(spawn_item)
			var interval = GameManager.rng_randi_range(info.spawn_interval_range.x,info.spawn_interval_range.y)
			spawn_task_tween.tween_interval(interval)
		var interval = GameManager.rng_randi_range(info.next_spawn_delay_range.x,info.next_spawn_delay_range.y)
		spawn_task_tween.tween_interval(interval)
	return spawn_task_tween
